package cn.itcast.service;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.mapper.MemberMapper;
import cn.itcast.mapper.OrderMapper;
import cn.itcast.mapper.OrderSettingMapper;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {


    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Result submit(Map map) throws Exception {

//        1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        //获取预约时间
        int setmealId = Integer.parseInt((String) map.get("setmealId"));//套餐id
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);//预约时间
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(date);
        if (orderSetting == null) {
            //当前时间未设置预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
//        2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        //获取可预约人数
        int number = orderSetting.getNumber();
        //获取已预约人数
        int reservations = orderSetting.getReservations();
        //判断该天是否预约已满
        if (reservations >= number) {
            //已经约满
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //检查当前用户是否为会员，如果是会员则直接完成预约
        String telephone = (String) map.get("telephone");
        Member member = memberMapper.findByTelephone(telephone);
        Member member1 = new Member();
//       检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约 则无法完成再次预约
        if (member != null) {
            Integer mapperId = member.getId();//会员id
            Order order = new Order(mapperId, date, setmealId);
            //根据会员id，预约时间，套餐id查询Order数据表
            Order findOrder = orderMapper.findByCondition(order);
            if (findOrder != null) {
                //重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        } else {
//        不是会员则自动完成注册并进行预约
            member1.setName((String) map.get("name"));
            member1.setPhoneNumber(telephone);
            member1.setIdCard((String) map.get("idCard"));
            member1.setSex((String) map.get("sex"));
            member1.setRegTime(new Date());
            memberMapper.add(member1);
        }
//        5、预约成功，更新当日的已预约人数
        Order order = new Order();
        if(member != null){
            order.setMemberId(member.getId());
        }else {
            order.setMemberId(member1.getId());
        }
        order.setOrderDate(date);
        order.setSetmealId(setmealId);
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //录入数据库
        orderMapper.add(order);
        //修改预约设计Reservations+1
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingMapper.editReservationsByOrderDate(orderSetting);
        //返回order.getId()
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderMapper.findById(id);
        if(map != null){
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }

        return map;
    }
}
